package com.fy.market.strategy.service.rule.tree.impl;

import com.fy.market.strategy.model.entity.RuleMatterEntity;
import com.fy.market.strategy.model.entity.StrategyAwardRuleEntity;
import com.fy.market.strategy.model.enums.RuleLogicCheckTypeEnum;
import com.fy.market.strategy.model.enums.RuleModelEnum;
import com.fy.market.strategy.model.enums.RuleModelTypeEnum;
import com.fy.market.strategy.model.entity.tree.RuleTreeResultEntity;
import com.fy.market.strategy.repository.IStrategyRepository;
import com.fy.market.strategy.service.rule.tree.ILogicTreeNode;
import com.fy.market.types.common.utils.FyObjUtil;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author fwq
 * @desc 规则锁节点
 * @date 2025年09月25
 */
@Component("rule_lock")
public class RuleLockLogicTreeNode implements ILogicTreeNode {

    @Resource
    private IStrategyRepository iStrategyRepository;

    @Override
    public RuleTreeResultEntity logic(RuleMatterEntity ruleMatterEntity) {
        // 构建响应
        RuleTreeResultEntity ruleTreeResultEntity = RuleTreeResultEntity
                .builder()
                .ruleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.ALLOW)
                .awardId(ruleMatterEntity.getAwardId())
                .build();

        // 锁规则为空，直接放行
        if (ruleMatterEntity.getRuleModel() == null || !RuleModelEnum.RULE_LOCK.equals(ruleMatterEntity.getRuleModel()))
            return ruleTreeResultEntity;

        List<StrategyAwardRuleEntity> strategyAwardRuleEntities = iStrategyRepository.queryStrategyAwardRuleEntities(ruleMatterEntity.getStrategyId());

        if (FyObjUtil.isNullObj(strategyAwardRuleEntities)) return ruleTreeResultEntity;

        Integer awardUnlockCount = strategyAwardRuleEntities.stream()
                .filter(strategyAwardRuleEntity -> ruleMatterEntity.getAwardId().equals(strategyAwardRuleEntity.getAwardId()))
                .findFirst()
                .map(strategyAwardRuleEntity -> {
                    // 检查是否有抽奖中规则且为奖品锁规则
                    boolean hasLockRule = strategyAwardRuleEntity.getRuleModels().stream()
                            .anyMatch(ruleModelEntity ->
                                    RuleModelTypeEnum.DURING_LOTTERY.getCode().equals(ruleModelEntity.getRuleModelType())
                                            && RuleModelEnum.RULE_LOCK.getName().equals(ruleModelEntity.getRuleModel()));
                    // 如果有锁规则，返回对应的awardSubtitle
                    if (hasLockRule)
                        return strategyAwardRuleEntity.getAwardUnlockCount();
                    // 如果没有锁规则，直接放行（返回null或空字符串）
                    return null;
                })
                .orElse(null);

        Integer userUnlockCount = 2;  //模拟用户抽奖次数统计，后续是从缓存获取数据库获取，目前先定死方便开发

        // 如果awardUnlockCount不为空，说明有锁规则，看看用户抽奖次数是否已达到，达到的话放行，不达到的话拦截
        if (awardUnlockCount == null || userUnlockCount >= awardUnlockCount)
            // 解锁成功，返回允许通过
            return ruleTreeResultEntity;

        // 解锁失败，返回拒绝通过
        ruleTreeResultEntity.setRuleLogicCheckTypeEnum(RuleLogicCheckTypeEnum.TAKE_OVER);

        return ruleTreeResultEntity;

    }
}
